'''
    Goblinoid: Experience all of MediaGoblin on an Android Device
    Copyright (C) 2015 Dylan Jeffers

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
'''

# models for app root
from __future__ import print_function
from os.path import join

from kivy.app import App
from kivy.properties import ObjectProperty, DictProperty, ListProperty
from kivy.uix.widget import Widget

from pypump.models.image import Image as PumpImage
from pypump import JSONStore
from pypump.exception import PyPumpException
# import requests


# store for user's credentials
class UserStore(JSONStore):
    @classmethod
    def get_filename(cls):
        return join(App.get_running_app().user_data_dir,
                    'MediaGoblin/credentials.json')


class PumpService(Widget):
    feed_content = ObjectProperty(None)
    pump = ObjectProperty(None)
    screen_dict = DictProperty()
    inbox = ListProperty()
    feed_data = ListProperty()

    def __init__(self, **kwargs):
        super(PumpService, self).__init__(**kwargs)
        self.init_screen_dict()
        self.data_dir = join(App.get_running_app().user_data_dir,
                             'MediaGoblin')
        self.init_screen_dict()

    def get_data_dir(self):
        return self.data_dir

    def init_screen_dict(self):
        # Note, the following imports depend on UserStore class
        # above, so importing them here for now
        from goblinoid.screens import loginview, actionview, screenviews
        self.screen_dict = dict([
            ('login', loginview.Login),
            ('webfinger', loginview.WebFinger),
            ('verify', loginview.Verify),
            ('enter', loginview.EnterApp),
            ('action', actionview.ActionLayout),
            ('settings', actionview.SettingsScreen),
            ('feed', screenviews.FeedView), ('media', screenviews.MediaView),
            ('profile', screenviews.ProfileView)
            ])

    def on_screen_dict(self, instance, value):
        print ('updated screen dict')

    def on_pump(self, instance, pump):
        print ('on_pump commencing')
        inbox = []
        data = []
        index = 0

# TODO may want to skip bad data rather than breaking out from for loop
        try:
            for activity in pump.me.inbox.major[:10]:
                inbox.append(activity)
                data_dict = self.build_data_dict(activity, index)
                data.append(data_dict)
                data_dict = {}
                index += 1
            self.inbox = inbox
            self.feed_data = data
        except KeyError, e:
            print('KeyError: {0}'.format(e))
            self.inbox = inbox
            self.feed_data = data

    def build_data_dict(self, activity, index):
        data_dict = dict([
            ('index', index),
            ('actor', str(activity.obj.author)),
            ('post_time', [str(activity.published.hour),
                           str(activity.published.minute)]),
            ('comments', self.display_comments(activity)),
            ('description', self.display_description(activity)),
            ('image_content', ''),
            ('else_content', '')
            ])
        data_dict.update(self.parse_activity_types(activity, index))
        return data_dict

    def parse_activity_types(self, activity, photo_index):
        if isinstance(activity.obj, PumpImage):
            return self.is_image_activity(activity, photo_index)
        else:
            return self.is_video_activity(activity)

    def is_image_activity(self, activity, photo_index):
        try:
            url = activity.obj.original.url
            return {"image_content": url}
        # Note: Experimental Code above, revert to this code if it
        # gives you issues
        #
        # try:
        #     url = activity.obj.original.url
        #     image_dir =
        # './downloaded_images/image{0}.png'.format(photo_index)
        #     fout = open(image_dir, 'wb')
        #     client = self.pump.setup_oauth_client()
        #     request = requests.get(url, auth=client)
        #     fout.write(request.content)
        #     print('got request')
        #     description = activity.obj.content
        #     if description is None:
        #         description = 'no description'
        #     return {("image_content", image_dir),
        #             ("description", description)}
        except AttributeError, e:
            return {"else_content": e}
        except Exception, e1:
            return {"else_content": e1}

    def is_video_activity(self, activity):
        else_text = 'cannot display audio/video yet'
        return {"else_content": else_text}

    def display_description(self, activity):
        try:
            description = activity.obj.display_name
            print (description)
            if description is None:
                description = activity.content
            return str(description)
        except AttributeError:
            return 'no description'

    def display_comments(self, activity):
        comment_array = []

        def build_comment_dict(author, content):
            return dict([
                        ("author", author),
                        ("content", content)
                        ])

        # Helpful for testing, remove afterward
        def comment_array_len(comment_array):
            print ('len: {0}'.format(len(comment_array)))

        try:
            activity.obj.comments
        except (AttributeError, Exception), e:
            print('display_comments: {0}'.format(e))
            comment_array_len(comment_array)
            return comment_array

        for comment in activity.obj.comments:
            try:
                author = str(comment.author)
                content = str(comment.content)
            except (UnicodeEncodeError, PyPumpException), e:
                print('display_comments: {0}'.format(e))
                author = 'Error'
                content = str(e)
            comment_array.append(build_comment_dict(author, content))
        comment_array_len(comment_array)
        return comment_array
